/*
 * Principal.java
 *
 * Created on 25/07/2009, 22:44:25
 */
package bil.gui;

import java.awt.*;
import java.lang.reflect.Method;
import javax.swing.ImageIcon;

/**
 *
 * @author Eduardo Folly
 */
public class SplashScreen extends javax.swing.JFrame {

    private static SplashScreen instance;

    /** Creates new form Principal */
    public SplashScreen(String local) {
        initComponents();
        try {
            ImageIcon ii = new ImageIcon(getClass().getResource(local));
            jLabelImage.setIcon(ii);
            int largura = ii.getIconWidth();
            int altura = ii.getIconHeight();
            java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
            this.setBounds((screenSize.width - largura) / 2, (screenSize.height - altura) / 2, largura, altura);
        } catch (Exception ex) {
            System.out.println("Erro ao adquirir imagem da splashscreen.");
        }

        // Transparencia
        try {
            Class<?> awtUtilitiesClass = Class.forName("com.sun.awt.AWTUtilities");
            Method mSetWindowOpacity = awtUtilitiesClass.getMethod("setWindowOpaque", Window.class, boolean.class);
            mSetWindowOpacity.invoke(null, this, false);
        } catch (Exception ex) {
            System.out.println("Erro ao iniciar transparencia.");
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelImage = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setAlwaysOnTop(true);
        setBackground(new java.awt.Color(255, 255, 255));
        setMinimumSize(new java.awt.Dimension(705, 490));
        setResizable(false);
        setUndecorated(true);

        jLabelImage.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabelImage)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabelImage)
        );
    }// </editor-fold>//GEN-END:initComponents

    public static void splash(String local) {
        if (instance == null) {
            instance = new SplashScreen(local);
            instance.setVisible(true);
        }
    }

    public static void disposeSplash() {
        if (instance != null) {
            instance.dispose();
            instance = null;
        }
    }

    public static void invokeMain(String className, String[] args) {
        try {
            boolean invoke = true;
            if (invoke) {
                Class.forName(className).getMethod("main", new Class[]{String[].class}).invoke(null, new Object[]{args});
            }
        } catch (Exception e) {
            InternalError error = new InternalError("Falha ao invocar método principal.");
            error.initCause(e);
            throw error;
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabelImage;
    // End of variables declaration//GEN-END:variables
}
